package com.winz.cultureapp.activity;

import android.content.Intent;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.winz.cultureapp.R;
import com.winz.cultureapp.base.BaseAppCompatActivity;
import com.winz.cultureapp.constants.Constants;
import com.winz.cultureapp.entity.ActivityBookDetailBean;
import com.winz.cultureapp.entity.AdminBookDetailBean;
import com.winz.cultureapp.http.OnResponeListener;
import com.winz.cultureapp.http.request.AdminBookDetailReq;
import com.winz.cultureapp.http.util.HttpUitls;
import com.winz.cultureapp.utils.JsonUtil;
import com.winz.cultureapp.utils.Logger;
import com.winz.cultureapp.utils.SharePrefsUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 扫码--活动订单详情
 */
public class AdminActivityOrderActivity extends BaseAppCompatActivity {
    @BindView(R.id.tv_top_title)
    TextView mTv_top_title;
    @BindView(R.id.tv_order_type)
    TextView mTv_order_type;
    @BindView(R.id.tv_no)
    TextView mTv_no;
    @BindView(R.id.tv_order_time)
    TextView mTv_order_time;
    @BindView(R.id.iv_pic)
    ImageView mIv_pic;
    @BindView(R.id.tv_price)
    TextView mTv_price;
    @BindView(R.id.tv_title)
    TextView mTv_title;
    @BindView(R.id.tv_location)
    TextView mTv_location;
    @BindView(R.id.tv_time)
    TextView mTv_time;
    @BindView(R.id.tv_tell)
    TextView mTv_tell;

    Handler handler = new Handler();

    private ActivityBookDetailBean.DataBean mData;
    private String randomstr;

    @Override
    protected int contentView() {
        return R.layout.activity_admin_activity_order;
    }

    @Override
    protected void initViews() {
        setStatusBar(getResources().getColor(R.color.base_color));
        mTv_top_title.setText("订单详情");

    }

    @Override
    protected void loadDatas(Intent intent) {
        if (intent != null){
            if (intent.getExtras() != null){
                mData = (ActivityBookDetailBean.DataBean) intent.getExtras().getSerializable(Constants.KEY.ADMIN.DETAIL);
                randomstr = intent.getExtras().getString(Constants.KEY.ADMIN.RANDOMSTR);
                if (mData != null){
                    updateUi();
                }
            }
        }
    }

    private void updateUi() {
        mTv_order_type.setText("已付款");
        mTv_no.setText("订单号：" + mData.activityOrder.orderNumber);
        mTv_order_time.setText("订单时间：" + mData.activityOrder.createTime);
        mTv_price.setText("￥ " + ((mData.activitySeat == null) || (mData.activitySeat.seatMoney == null)?mData.activity.money:mData.activitySeat.seatMoney));
        mTv_title.setText(mData.activity.title);

        mTv_location.setText("地址：" + mData.activity.unit.address);
        mTv_time.setText("时间：" + mData.activity.startTime.substring(0,16) + " - " + mData.activity.endTime.substring(0,16));
        mTv_tell.setText("座位：" + (mData.activitySeat == null || (mData.activitySeat.seatDesc == null)?"无" : mData.activitySeat.seatDesc));
        //圆角处理
        Glide.with(this)
                .load(mData.activity.unit.imageUrl)
                .error(R.drawable.pic_default)
                .into(mIv_pic);
    }

    @OnClick({R.id.fl_back,R.id.tv_ok})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fl_back:
                finish();
                break;
            case R.id.tv_ok:
                commit();
                break;

        }
    }

    private void commit() {
        showProgressDialog();

        HttpUitls.onPostByToken("admin_user_activity_book_verify", SharePrefsUtil.getInstance().getString(Constants.USER_INFO.TOKEN), new AdminBookDetailReq(randomstr), new OnResponeListener<AdminBookDetailBean>() {
            @Override
            public void onSuccess(AdminBookDetailBean bean) {
                Logger.d("admin_user_activity_book_verify==>>", JsonUtil.getJsonFromObj(bean));
                if (bean.code == 0) {
                    showMsg("认证成功");
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            finish();
                        }
                    },1000);

                }else{
                    showMsg(bean.msg);
                }
                dismissProgressDialog();
            }

            @Override
            public void onFailed(String error) {
                Logger.d("admin_user_activity_book_verify==>>", error);
                dismissProgressDialog();
            }
        });
    }
}
